﻿using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;

namespace JsonDialog
{
    public class Hero
    {
        private class RuntimeClass
        {
            public bool PropertiesSet = false;
            public string HeroName = "Bob";
            public DateTime CurrentTime 
            {
                get 
                {
                    return DateTime.Now;
                }
            }
            public Gender Gender = Gender.Male;



        }

        RuntimeClass Runtime = new RuntimeClass();

        public void AskForProperties()
        {
            if (Runtime.PropertiesSet) throw new InvalidOperationException("yeah why dont you go suck a dickens");
            Console.Clear();
            Console.Write(@"Character name> ");
            Runtime.HeroName = Console.ReadLine().Trim();
            Console.Write(@"Character gender (Male/Female)> ");
            Runtime.Gender = (Gender)Enum.Parse(typeof(Gender), Console.ReadLine().Trim(), true);
        }

        public void StartNpcDialog(string npcId)
        {
            DialogController Npc = new DialogController(npcId);
            


        }

        public static void Main()
        {
            Hero TheChosenOne = new Hero();
            //TheChosenOne.AskForProperties();
            TheChosenOne.StartNpcDialog("testnpc_starky");
            TextInlineParser.SetConfig(new InlineParserConfig { HeroName = TheChosenOne.Runtime.HeroName, HeroGender = TheChosenOne.Runtime.Gender });
            Console.WriteLine(TextInlineParser.ParseString(@"%RandomFrom[Hey!,What's up?] What a lovely %DayQuarter[night,morning,day,evening], isn't it?"));
            Console.ReadKey();
        }
    }

    class DialogController
    { 
        private class RuntimeClass
        {
            public bool IdSet = false;
            public string Id = "";
            public string Filename = "testFile.json";
            public JObject NpcData = null;
        }

        private static class SharedRuntimeClass
        {
            public static Dictionary<string, int> NpcMeetingCount = new Dictionary<string, int>();
        }

        RuntimeClass Runtime = new RuntimeClass();

        public DialogController(string npcId)
        {
            SetId(npcId);
            RetrieveData();

        }

        /// <summary>
        /// Sets Id of the NPC whose dialog is to be loaded. Can only be used once.
        /// </summary>
        /// <param name="npcId"></param>
        public void SetId(string npcId)
        {
            if (Runtime.IdSet) throw new InvalidOperationException("DialogController NPC Id can only be set once");
            Runtime.Id = npcId;
            Runtime.IdSet = true;
        }

        public void RetrieveData()

        {
            JArray Doc = JArray.Parse(File.ReadAllText(Runtime.Filename));
            Runtime.NpcData = (JObject)Doc.First(k => k["id"].ToString() == Runtime.Id);
        }

    }

    public enum Gender
    {
        Male,
        Female
    }
    
    public enum Race
    {
        EarthPony,
        Pegasus,
        Unicorn,
        Alicorn,
        Zebra
    }

    public struct InlineParserConfig
    {
        public string HeroName;
        public Gender HeroGender;

        public string NpcName;
        public Gender NpcGender;

        public DateTime Time;

        public static InlineParserConfig Default = new InlineParserConfig
        {
            HeroName = "Bob",
            HeroGender = Gender.Male,
            NpcName = "Bubba",
            NpcGender = Gender.Female,
            Time = DateTime.Now
        };
    }

}
